package com.ruoyi.wms.controller.stock;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.idempotent.annotation.RepeatSubmit;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.web.core.BaseController;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.wms.domain.vo.ConversionRulesVo;
import com.ruoyi.wms.domain.bo.stock.ConversionRulesBo;
import com.ruoyi.wms.service.ConversionRulesService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

/**
 * 转换关系表
 *
 * @author 邓滔滔
 * @date 2025-01-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/transformrules")
public class ConversionRulesController extends BaseController {

    private final ConversionRulesService conversionRulesService;

    /**
     * 查询转换关系表列表
     */
    @SaCheckPermission("wms:transformrules:list")
    @GetMapping("/list")
    public TableDataInfo<ConversionRulesVo> list(ConversionRulesBo bo, PageQuery pageQuery) {
        return conversionRulesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出转换关系表列表
     */
    @SaCheckPermission("wms:transformrules:export")
    @Log(title = "转换关系表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ConversionRulesBo bo, HttpServletResponse response) {
        List<ConversionRulesVo> list = conversionRulesService.queryList(bo);
        ExcelUtil.exportExcel(list, "转换关系表", ConversionRulesVo.class, response);
    }

    /**
     * 获取转换关系表详细信息
     *
     * @param ruleId 主键
     */
    @SaCheckPermission("wms:transformrules:query")
    @GetMapping("/{ruleId}")
    public R<ConversionRulesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long ruleId) {
        return R.ok(conversionRulesService.queryById(ruleId));
    }

    /**
     * 新增转换关系表
     */
    @SaCheckPermission("wms:transformrules:add")
    @Log(title = "转换关系表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ConversionRulesBo bo) {
        conversionRulesService.insertByBo(bo);
        return R.ok();
    }

    /**
     * 修改转换关系表
     */
    @SaCheckPermission("wms:transformrules:edit")
    @Log(title = "转换关系表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ConversionRulesBo bo) {
        conversionRulesService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除转换关系表
     *
     * @param ruleIds 主键串
     */
    @SaCheckPermission("wms:transformrules:remove")
    @Log(title = "转换关系表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ruleIds) {
        conversionRulesService.deleteByIds(List.of(ruleIds));
        return R.ok();
    }
}
